package com.hzjy.api.controller;

import com.hzjy.api.beans.HzjyFileUploadRecord;
import com.hzjy.api.service.impl.HzjyFileUploadRecordServiceImpl;
import com.hzjy.common.config.RuoYiConfig;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.utils.JSONUtil;
import com.hzjy.common.utils.WordUtils;
import com.hzjy.common.utils.file.FileUploadUtils;
import com.hzjy.framework.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 文件上传
 */

@RestController
@RequestMapping("/api/file")
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(ApiPayController.class);
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private HzjyFileUploadRecordServiceImpl hzjyFileUploadRecordServiceImpl;

    @PostMapping("/imageUpload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        log.info("文件上传:" + JSONUtil.object2json(file));
        Boolean flag ;
        String pdfUrl = null;
        String url = null;
        String videoUrl = null;
        String audioUrl = null;
        HzjyFileUploadRecord hzjyFileUploadRecord = new HzjyFileUploadRecord();
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            log.info("----------------" + fileName);
            //截取最后的上传文件格式
            String substring = fileName.substring(fileName.lastIndexOf("."));
            log.info("----------------" + substring);
            if (substring.equals(".pdf")){
                flag = true;
                //url = serverConfig.getUrl() + fileName;
                url = "http://img.banquango.com" + fileName;
                pdfUrl = "http://img.banquango.com/pdf.jpg";
                hzjyFileUploadRecord.setFileUrl(url);
            }else if (substring.equals(".mp4")){
                flag = true;
                //videoUrl = serverConfig.getUrl() + fileName;
                videoUrl = "http://img.banquango.com" + fileName;
                url = "http://img.banquango.com/shipin.png";
                hzjyFileUploadRecord.setFileUrl(videoUrl);
            }else if (substring.equals(".mp3")){
                flag = true;
                //audioUrl = serverConfig.getUrl() + fileName;
                audioUrl = "http://img.banquango.com" + fileName;
                url = "http://img.banquango.com/yinpin.png";
                hzjyFileUploadRecord.setFileUrl(audioUrl);
            }else if(substring.equals(".bmp") || substring.equals(".gif") || substring.equals(".jpg") || substring.equals(".jpeg") || substring.equals(".png") ){
                flag = false;
                //url = serverConfig.getUrl() + fileName;
                url = "http://img.banquango.com" + fileName;
                hzjyFileUploadRecord.setFileUrl(url);
            }else {
                return AjaxResult.error(500,"上传文件格式错误");
            }
            hzjyFileUploadRecord.setUploadTime(new Date());
            hzjyFileUploadRecordServiceImpl.insertHzjyFileUploadRecord(hzjyFileUploadRecord);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            ajax.put("fileId", hzjyFileUploadRecord.getId());
            ajax.put("flag",flag);
            ajax.put("pdfUrl",pdfUrl);
            ajax.put("videoUrl",videoUrl);
            ajax.put("audioUrl",audioUrl);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 把信息放到jsp展示，
     */
    /*@RequestMapping("/preview")
    public ModelAndView preview(HttpServletRequest request) {
        //查询数据库获取文件信息
        //根据自己需求来定义查询方法这里是简单的查询
        //TabCoreNoticeEntity coreNoticeEntity = noticeService.select();  查询在实体类中的全部信息，包括文件名
        String fileUrl=RuoYiConfig.getUploadPath();
        String inPath =serverConfig.getUrl() + fileUrl;
        System.out.println(inPath);
        //将文件转为pdf，并返回pdf的路径
        //word转为pdf进行预览
        String outPath ="文件输出位置，用户可以访问到的路径：例如：webapp下的file文件夹";
        try {
            WordUtils.word2pdf(inPath, outPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去掉文件的后缀名
        String name=fileUrl.substring(0,fileUrl.lastIndexOf("."));
        //把文件改为pdf格式
        String realPdfUrl=name+".pdf";
        //修改此地址：上线时在配置文件中修改为服务器地址
        String pdfFileName=WEB_DOMAIN+"/img/"+realPdfUrl;
        System.out.println(pdfFileName);
        modelAndView.addObject("url",pdfFileName);
        modelAndView.setViewName("要返回的页面");
        return modelAndView;
    }*/

    /**
     * 小程序上传接口
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/WXImageUpload")
    public AjaxResult WXUploadFile(MultipartFile file) throws Exception {
        log.info("文件上传:" + JSONUtil.object2json(file));
        Boolean flag ;
        String pdfUrl = null;
        String url = null;
        HzjyFileUploadRecord hzjyFileUploadRecord = new HzjyFileUploadRecord();
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.wxUpload(filePath, file);
            log.info("----------------" + fileName);
            //截取最后的上传文件格式
            String substring = fileName.substring(fileName.lastIndexOf("."));
            log.info("----------------" + substring);
            if (substring.equals(".pdf")){
                flag = true;
                url = "http://img.banquango.com" + fileName;
                pdfUrl = "http://img.banquango.com/pdf.jpg";
                hzjyFileUploadRecord.setFileUrl(url);
            }else if(substring.equals(".bmp") || substring.equals(".gif") || substring.equals(".jpg") || substring.equals(".jpeg") || substring.equals(".png") ){
                flag = false;
                url = "http://img.banquango.com" + fileName;
                hzjyFileUploadRecord.setFileUrl(url);
            }else {
                return AjaxResult.error(500,"上传文件格式错误");
            }
            hzjyFileUploadRecord.setUploadTime(new Date());
            hzjyFileUploadRecordServiceImpl.insertHzjyFileUploadRecord(hzjyFileUploadRecord);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            ajax.put("fileId", hzjyFileUploadRecord.getId());
            ajax.put("flag",flag);
            ajax.put("pdfUrl",pdfUrl);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
